package com.example.contextcube;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.contextcube.base.BaseActivity;
import com.example.contextcube.db.DictionaryDatabaseHelper;
import com.example.contextcube.util.FavoriteManager;
import com.example.contextcube.util.SharedPrefsUtil;
import com.example.contextcube.util.ToastManager;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private static final String TAG = "MainActivity";
    private DictionaryDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 检查登录状态
        if (!SharedPrefsUtil.isLoggedIn(this)) {
            // 未登录，返回登录页面
            startActivity(new Intent(this, LoginActivity.class));
            finish();
            return;
        }

        // 设置底部导航
        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(this);
        
        // 初始化数据库
        dbHelper = new DictionaryDatabaseHelper(this);
        
        // 初始化FavoriteManager
        FavoriteManager favoriteManager = FavoriteManager.getInstance();
        favoriteManager.setContext(this);
        favoriteManager.setDatabaseHelper(dbHelper);

        // 默认显示词典页面
        if (savedInstanceState == null) {
            try {
                loadFragment(new DictionaryFragment());
            } catch (Exception e) {
                Log.e(TAG, "加载词典Fragment失败", e);
                ToastManager.showShort(this, "加载页面失败");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 关闭数据库连接
        if (dbHelper != null) {
            dbHelper.close();
        }
    }

    /**
     * 底部导航菜单点击处理
     */
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;
        
            switch (item.getItemId()) {
                case R.id.navigation_dict:
                selectedFragment = new DictionaryFragment();
                    break;
                case R.id.navigation_favorites:
                selectedFragment = new FavoritesFragment();
                    break;
                case R.id.navigation_settings:
                selectedFragment = new SettingsFragment();
                    break;
        }
        
        return loadFragment(selectedFragment);
    }
    
    /**
     * 加载Fragment
     */
    private boolean loadFragment(Fragment fragment) {
        if (fragment != null) {
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.fragment_container, fragment)
                        .commit();
                return true;
        }
        return false;
    }
}